package com.zzl.alligator.management.system.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.lang.UUID;
import com.zzl.alligator.common.constant.Constants;
import com.zzl.alligator.common.util.ServletUtils;
import com.zzl.alligator.framework.cache.ICache;
import com.zzl.alligator.framework.cache.support.CacheSpecEnum;
import com.zzl.alligator.framework.security.LoginUser;
import com.zzl.alligator.framework.security.service.TokenService;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysMenu;
import com.zzl.alligator.management.system.domain.SysUser;
import com.zzl.alligator.management.system.service.ISysMenuService;
import com.zzl.alligator.management.system.service.ISysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录认证
 *
 * @author zhouziliang
 * @since 2020/6/28
 */
@RestController
@Slf4j
public class SysLoginController {

    @Autowired
    private ICache cache;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysPermissionService permissionService;
    @Autowired
    private ISysMenuService menuService;


    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public JsonResult getCode(HttpServletResponse response) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(111, 35, 4, 4);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        captcha.write(outputStream);
        log.info("生成验证码：{}", captcha.getCode());
        String uuid = UUID.fastUUID().toString();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        cache.setCacheObject(CacheSpecEnum.VARIFY_CODE, verifyKey, captcha.getCode());

        JsonResult success = JsonResult.success();
        Map<String, String> map = new HashMap<>(2);
        map.put("uuid", verifyKey);
        map.put("img", captcha.getImageBase64());
        success.setData(map);
        return success;
    }

    @GetMapping("/userInfo")
    public JsonResult getInfo(HttpServletRequest httpServletRequest) {
        LoginUser loginUser = tokenService.getLoginUser(httpServletRequest);
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        //用户辅助信息

        Map<String, Object> map = new HashMap<>(3);
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return JsonResult.success(map);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/routers")
    public JsonResult getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.findMenuTreeByUserId(user.getUserId());
        return JsonResult.success(menuService.buildMenus(menus));
    }

}
